package com.controller;


import com.domain.User;
import com.service.UserService;
import com.util.ResulDate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsPackage: com.hq1.javaweb.controller
 * @Author: jiangjunjie
 * @CreateTime: 2025-02-19  14:17
 * @Description: TODO
 * @Version: 1.8
 */
@RestController
public class UserController {
    @Resource
    UserService userService;
    @RequestMapping("/index")
        public String getUserList(Model model){
            List<User> userList = userService.getUserList();
            model.addAttribute("userList",userList);
            return "index";
        }
    @RequestMapping("/addUser")
    @ResponseBody
        public ResulDate addUser(User user){
        int i = userService.addUser(user);
        ResulDate<User> resulDate = new ResulDate<>();
        if(i>0){
            resulDate.setCode(200);
            resulDate.setMsg("添加成功");
        }else {
            resulDate.setCode(500);
            resulDate.setMsg("添加失败");
        }

        return resulDate;
        }
    @RequestMapping("/del")
    @ResponseBody
    public ResulDate delUser(int id){
        int i = userService.delUserById(id);
        ResulDate<User> resulDate = new ResulDate<>();
        if(i>0){
            resulDate.setCode(200);
            resulDate.setMsg("删除成功");
        }else {
            resulDate.setCode(500);
            resulDate.setMsg("删除失败");
        }
        return resulDate;

    }
    @RequestMapping("/update")
    @ResponseBody
    public ResulDate updateUser(User user){
        int i = userService.updateUser(user);
        ResulDate<User> resulDate = new ResulDate<>();
        if(i>0){
            resulDate.setCode(200);
            resulDate.setMsg("修改成功");
        }else {
            resulDate.setCode(500);
            resulDate.setMsg("修改失败");
        }
        return resulDate;
    }

    @RequestMapping("/show")
    public String showUser(int id,Model model){
        User user = userService.getUserByIds(id);
        model.addAttribute("user",user);
        return "showUser";
    }

}
